package com.qianfeng.missfood.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.qianfeng.missfood.BaseApp;
import com.qianfeng.missfood.R;
import com.qianfeng.missfood.bean.User;
import com.qianfeng.missfood.constants.AppConfig;
import com.qianfeng.missfood.db.DBHelper;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {


    private EditText loginPhone;
    private EditText loginPsw;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        ImageView backToHome = (ImageView) findViewById(R.id.backToHome);
        backToHome.setOnClickListener(this);
        loginPhone = (EditText) findViewById(R.id.loginPhone);
        loginPsw = (EditText) findViewById(R.id.loginPsw);

        Button forgetPsw = (Button) findViewById(R.id.forgetPsw);
        forgetPsw.setOnClickListener(this);

        Button btnLogin = (Button) findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(this);
        Button btnRegister = (Button) findViewById(R.id.btnRegister);
        btnRegister.setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.backToHome:

                onBackPressed();
                break;
            case R.id.forgetPsw:

                startAction(FindPswActivity.class);
                break;

            case R.id.btnLogin:

                String phone = loginPhone.getText().toString().trim();
                String psw = loginPsw.getText().toString().trim();
                if ("".equals(phone)) {

                    Toast.makeText(this, "请填写手机号", Toast.LENGTH_SHORT).show();
                } else if ("".equals(psw)) {

                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                } else {

                    User user = new User(phone, psw);
                    boolean isUser = DBHelper.DbUser.selectUser(user);
                    if (isUser) {

                        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                        BaseApp.getInstance().setLoginState(true);
                        BaseApp.getInstance().setUser(user);
                        onBackPressed();

                    } else {

                        Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                    }

                }

                break;

            case R.id.btnRegister:

                startAction(RegisterActivity.class);
                break;

            default:
                break;

        }

    }

    private void startAction(Class<?> cls) {

        Intent intent = new Intent(this, cls);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        setResult(AppConfig.LOGIN_RESULT);
    }
}




